package com.ytjj.qmyx.mall.service.impl;

import com.ytjj.common.constants.QueueConstants;
import com.ytjj.common.dto.mqdto.SkipStatisticsDto;
import com.ytjj.qmyx.mall.mapper.SkipStatisticsMapper;
import com.ytjj.qmyx.mall.model.SkipStatistics;
import com.ytjj.qmyx.mall.service.SkipStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 
 * @version 1.0
 *
 */
@Service
@Slf4j
public class SkipStatisticsServiceImpl implements SkipStatisticsService {

    @Resource
    private SkipStatisticsMapper skipStatisticsMapper;

    @Resource
    private RabbitTemplate rabbitTemplate;
    Executor pools = new ThreadPoolExecutor(5, 10, 5,
            TimeUnit.SECONDS, new LinkedBlockingQueue<>(1024 * 1024));

    @Override
    public void saveSkipStatistics(String uri, String sessionId, String channel) {
        SkipStatisticsDto skipStatisticsDto = new SkipStatisticsDto(channel,sessionId,new Date(),uri);
        try {
            pools.execute(() -> {
                rabbitTemplate.convertAndSend(QueueConstants.SKIP_STATISTICS_QUEUE,skipStatisticsDto);
            });
        } catch (Exception e) {
            log.error("==={}", e);
        }
    }
}
